﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

namespace ProyectoLab4
{
    public partial class MailNuevo : System.Web.UI.Page
    {
        GestionMails Mail = new GestionMails();
        GridView TraerMail = new GridView();
        String loginUsuario;
        String consulta;

        protected void Page_Load(object sender, EventArgs e)
        {
            loginUsuario = Request.Cookies["Usuario"].Value;
            txtUsuario.Text = loginUsuario;
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    consulta = "SELECT * FROM Bandeja_Mails WHERE id_mail='" + Request.QueryString["id"] + "'";
                    Mail.CargarBandeja(consulta, ref TraerMail);
                    if (TraerMail.Rows[0].Cells[1].Text == loginUsuario)
                    {
                        txtDestinatario.Text = TraerMail.Rows[0].Cells[2].Text;
                    }
                    else
                    {
                        txtDestinatario.Text = TraerMail.Rows[0].Cells[1].Text;
                    }
                    txtAsunto.Text = "RE: " + TraerMail.Rows[0].Cells[3].Text;
                    txtMensaje.Text = "\n\n-----------\n" + TraerMail.Rows[0].Cells[4].Text + "\n\n" + TraerMail.Rows[0].Cells[5].Text;
                    txtMensaje.Focus();
                }
            }
        }


        protected void btnEnviar_Click(object sender, EventArgs e)
        {
            int cont = 0;
            GestionUsuarios gestionUser = new GestionUsuarios();

            if (txtDestinatario.Text == "")
            {
                lblErrorDestinatario.Visible = true;
                cont++;
            }

            if (txtAsunto.Text == "")
            {
                lblErrorAsunto.Visible = true;
                cont++;
            }

            if (cont == 0)
            {
                if (gestionUser.EstaRegistrado(txtDestinatario.Text) == true)
                {
                    Mail.GrabarMail(loginUsuario, txtDestinatario.Text, txtAsunto.Text, txtMensaje.Text);
                    
                    PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
                    isreadonly.SetValue(this.Request.QueryString, false, null);
                    this.Request.QueryString.Remove("id");

                    Server.Transfer("Mails.aspx");
                    
                }
                else
                {
                    lblErrorDestinatario.Visible = true;
                }
            }
        }

        protected void btnGuardarBorrador_Click(object sender, EventArgs e)
        {
            int cont = 0;

            if (txtDestinatario.Text == "")
            {
                lblErrorDestinatario.Visible = true;
                cont++;
            }

            if (txtAsunto.Text == "")
            {
                lblErrorAsunto.Visible = true;
                cont++;
            }

            if (cont == 0)
            {
                Borrador m = new Borrador(loginUsuario, txtDestinatario.Text, txtAsunto.Text, txtMensaje.Text);
                Mail.GrabarBorrador(m);
                Server.Transfer("Mails.aspx");
            }
        }
    }
}